import Vue from "vue";
import VueRouter from "vue-router";
import Login from "../views/Login.vue";

Vue.use(VueRouter);
import { message } from "ant-design-vue";

const routes = [
  {
    path: "/",
    name: "login",
    component: Login,
  },
  {
    path: "/login",
    name: "Login",
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () =>
      import(/* webpackChunkName: "about" */ "../views/Login.vue"),
  },
  {
    path: "/register",
    name: "register",
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () =>
      import(/* webpackChunkName: "about" */ "../views/Register.vue"),
  },
  {
    path: "/index",
    name: "index",
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    redirect: "/index/adhoc-payment-admin",
    component: () =>
      import(/* webpackChunkName: "about" */ "../views/index.vue"),

    children: [
      {
        path: "adhoc-payment-admin",
        name: "adhoc-payment-admin",
        component: () =>
          import(
            /* webpackChunkName: "about" */ "../views/adhoc-payment-admin.vue"
          ),
      },
      {
        path: "visitor-history",
        name: "visitor-history",
        component: () =>
          import(
            /* webpackChunkName: "about" */ "../views/visitor-history.vue"
          ),
      },
      {
        path: "dataShow",
        name: "dataShow",
        component: () =>
          import(/* webpackChunkName: "about" */ "../views/dataShow.vue"),
      },
      {
        path: "admin",
        name: "admin",
        component: () =>
          import(/* webpackChunkName: "about" */ "../views/admin.vue"),
      },
    ],
  },
  {
    path: "/about",
    name: "about",
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () =>
      import(/* webpackChunkName: "about" */ "../views/about.vue"),
  },
  // 隐私
  {
    path: "/privacy",
    name: "privacy",
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () =>
      import(/* webpackChunkName: "about" */ "../views/privacy.vue"),
  },
  // 服务条款
  {
    path: "/otherRules",
    name: "otherRules",
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () =>
      import(/* webpackChunkName: "about" */ "../views/otherRules.vue"),
  },
  {
    path: "*",
    name: "404",
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "about" */ "../views/404.vue"),
  },
];

const router = new VueRouter({
  mode: "hash",
  base: process.env.BASE_URL,
  routes,
});

//注册全局前置守卫
// 全局路由守卫 拦截所有路由

router.beforeEach((to, from, next) => {
  // 获取token
  const token = localStorage.getItem("authorization");
  // 有token
  if (token) {
    next();
  } else {
    // 否则是没有
    // 如果去的是登录页
    if (to.path === "/login") {
      // 直接放行
      next();
    } else if (to.path === "/register") {
      // 直接放行
      next();
    } else {
      // 如果去的是其他页,跳转到登录页
      message.error("Please Login！");
      // 跳转到登录页
      return next({ path: "/login" });
    }
  }
});

// 动态添加路由的方法
export function routerPageAdd(data) {
  let tempData = [];
  data.forEach((element) => {
    tempData.push({
      path: element.path,
      name: element.name,
      // route level code-splitting
      // this generates a separate chunk (about.[hash].js) for this route
      // which is lazy-loaded when the route is visited.
      component: () =>
        import(/* webpackChunkName: "about" */ "../views/" + element.component),
    });
  });
  // 动态添加路由
  return router.addRoutes(tempData);
}

export default router;
